package com.zuoban.exception;

import com.zuoban.constant.HttpStatus;
import com.zuoban.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Slf4j
@RestControllerAdvice
public class GlobalControllerExceptionAdvice {

    @ExceptionHandler(value = AuthorizationException.class)
    public Result authorizationException(HttpServletRequest request){
        HashMap<String, Object> map = new HashMap<>();
        map.put("errorMsg","您没有访问当前url的权限");
        return new Result(HttpStatus.HTTP_NOT_AUTHORITATIVE,"授权时出现的异常",map);
    }

    @ExceptionHandler(value = RuntimeException.class)
    public Result runtimeException(HttpServletRequest request,RuntimeException e){

        log.error("出现运行时的异常，详细堆栈信息展示：");
        StackTraceElement element = e.getStackTrace()[0];
        log.error("类名：{}，方法名：{}，代码所在行：{}，异常报错：{}", element.getClassName(),element.getMethodName(),element.getLineNumber(),e.toString());

        HashMap<String, Object> map = new HashMap<>();
        map.put("errorMsg",e.getMessage());
        return new Result(HttpStatus.HTTP_NOT_AUTHORITATIVE,"运行时出现的异常",map);
    }
}
